package com.cs4590.finalProject.userInterface;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cs4590.finalProject.AudioRecorder;
import com.cs4590.finalProject.ObjectDatabase;
import com.cs4590.finalProject.R;
import com.cs4590.finalProject.YouPiano;

public class Record extends Activity {

	ObjectDatabase od;

	AudioRecorder ar;

	boolean started = false;
	ImageButton record;
	boolean finished = false;
	
	EditText edit;
	String name;

	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.page_record);
		record = (ImageButton) findViewById(R.id.start);
		
		YouPiano application = (YouPiano) this.getApplication();
		od = application.getOd();
		
		edit = (EditText) findViewById(R.id.fileName);
		showFiles();
	}

	public void start(View v) {
		name = edit.getText().toString();

		od.store(name);

		if(name.equals("")) {
			Toast.makeText(this, "Please enter a file name.", Toast.LENGTH_LONG).show();
		}
		else {
			ar = new AudioRecorder(name);

			try {
				record.setBackgroundResource(R.drawable.button_recording);
				ar.start();
				started = true;
			} catch (Exception e) {
				Toast.makeText(this, "Error with starting recording.", Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}
		}
	}
	public void stop(View v) {
		if(started) {
			try {
				record.setBackgroundResource(R.drawable.button_record);
				ar.stop();
			} catch (Exception e) {
				Toast.makeText(this, "Error with stopping recording.", Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}
			finished = true;
		} else {
			Toast.makeText(this, "Recording must be started before it can be stopped.", Toast.LENGTH_LONG).show();
		}
	}
	public void play(View v) {
		if(finished) {
			try {
				ar.play(this);
			} catch (Exception e) {
				Toast.makeText(this, "Error with playing recording.", Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}
		} else {
			if(!started) 
				Toast.makeText(this, "Can't play file if it isn't recorded yet.", Toast.LENGTH_LONG).show();
			else 
				Toast.makeText(this, "Stop recording to hear playback.", Toast.LENGTH_LONG).show();
		}
	}

	public void save(View v) {
		//		ArrayList<String> names = od.getAll();
		//		od.delete(names.get(0));
		edit.setText("");
		ar = null;
		started=false;
		finished=false;
		showFiles();
//		Toast.makeText(this, od.getAll().toString(), Toast.LENGTH_LONG).show();
	}
	
	private void showFiles() {
		try {
			LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout_record);
			layout.removeAllViews();
			
			ArrayList<String> names = od.getAll();
			
			for(String name : names) {
				TextView temp = new TextView(this);
				temp.setTextColor(Color.BLACK);
				temp.setTextSize(20);
				temp.setText(name);
				layout.addView(temp);
			}
			TextView temp = new TextView(this);
			temp.setTextColor(Color.BLACK);
			temp.setTextSize(20);
			temp.setText("default");
			layout.addView(temp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
